//
//  KZCategoryCell.m
//  xiaorizi
//
//  Created by kun on 15/10/26.
//  Copyright © 2015年 origin. All rights reserved.
//

#import "KZCategoryCell.h"
#import "Masonry.h"
#import "UIViewAdditions.h"
#import "KZCategoryView.h"

@interface KZCategoryCell ()


@property (nonatomic,copy) NSMutableArray *viewArray;

@property (weak, nonatomic) IBOutlet UILabel *titleLabel;


@end

@implementation KZCategoryCell

- (void)awakeFromNib {
    // Initialization code
    //取消cell 点击的颜色提示
    self.selectionStyle = UITableViewCellSelectionStyleNone;
    
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}



//初始化数据
-(void)setModel:(CategoryModel *)model
{
    _model = model;
    
    //1

    NSArray *tagsArray = model.tags;
    
    self.titleLabel.text = model.title;
    
    _viewArray = [NSMutableArray array];
    
    //创建窗口
    
    for(NSInteger i = 0; i < tagsArray.count; i ++)
    {
        KZCategoryView *view = [KZCategoryView categoryView];
        [self addSubview:view];
        //view.backgroundColor = [UIColor blueColor];
        [_viewArray addObject:view];
        view.tag = i;
        
        //2 给view 添加点击手势
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(categoryViewClicked:)];
        [view addGestureRecognizer:tap];

    }
    

    int totalloc=3;
    CGFloat appvieww= ([UIScreen mainScreen].bounds.size.width - 16)/3;
    CGFloat appviewh= appvieww*0.8;
    CGFloat margin= 4;

    for(int i = 0; i < _viewArray.count; i ++)
    {
        KZCategoryView * view  = _viewArray[i];
        int row = i/totalloc;//行号
        int loc=i%totalloc;//列号
        CGFloat appviewx=margin+(margin+appvieww)*loc;
        CGFloat appviewy=margin+(margin+appviewh)*row;
        view.frame = CGRectMake(appviewx, appviewy + 45, appvieww, appviewh);

        CategoryEventModel *model = tagsArray[i];
        //2 传递数据
        view.model = model;
    }
}

#pragma 点击手势触发
-(void)categoryViewClicked:(UITapGestureRecognizer *)tap
{
    //获取点击的主题
  
    
    
    NSInteger tag = tap.view.tag;
    
    NSArray *tagsArray = _model.tags;
    CategoryEventModel *model = tagsArray[tag];

    if(self.categoryViewBlock)
    {
        self.categoryViewBlock(self,tag,model.name);
    }
    
}




@end
